package csu.web.jpetstore.web.servlet;

import csu.web.jpetstore.domain.Order;
import csu.web.jpetstore.persistence.impl.LoggerDAOImpl;
import csu.web.jpetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static csu.web.jpetstore.persistence.impl.LoggerDAOImpl.loginLogger;

public class ConfirmOrderViewServlet extends HttpServlet {

    private static final String CONFIRM_ORDER_FORM = "/WEB-INF/jsp/order/confirmOrderView.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Order order = (Order) session.getAttribute("order");
        //订单生成时间
        Date date = new Date();
        order.setOrderDate(date);

        order.setCardType(req.getParameter("order.creditCardType"));
        order.setCreditCard(req.getParameter("order.creditCard"));
        order.setExpiryDate(req.getParameter("order.expiryDate"));

        order.setBillToFirstName(req.getParameter("order.billToFirstName"));
        order.setBillToLastName(req.getParameter("order.billToLastName"));
        order.setBillAddress1(req.getParameter("order.billAddress1"));
        order.setBillAddress2(req.getParameter("order.billAddress2"));
        order.setBillCity(req.getParameter("order.billCity"));
        order.setBillState(req.getParameter("order.billState"));
        order.setBillZip(req.getParameter("order.billZip"));
        order.setBillState(req.getParameter("order.billState"));
        order.setBillCountry(req.getParameter("order.billCountry"));

        order.setShipToFirstName(req.getParameter("orderShipToFirstName"));
        order.setShipToLastName(req.getParameter("orderShipToLastName"));
        order.setShipAddress1(req.getParameter("orderShipAddress1"));
        order.setShipAddress2(req.getParameter("orderShipAddress2"));
        order.setShipCity(req.getParameter("orderShipCity"));
        order.setShipState(req.getParameter("orderShipState"));
        order.setShipZip(req.getParameter("orderShipZip"));
        order.setShipCountry(req.getParameter("orderShipCountry"));

        LoggerDAOImpl loggerDAO=new LoggerDAOImpl();
        loginLogger.setStarttime();
        loginLogger.setAction(loginLogger.getUsername()+"提交订单");
        try {
            loggerDAO.logger(loginLogger);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //将订单信息插入数据库
        OrderService orderService = new OrderService();
        order.setOrderId(orderService.insertOrder(order));
        String orderId = Integer.toString(order.getOrderId());
        session.setAttribute("orderId", orderId);
        session.setAttribute("order", order);

        //清空购物车
        session.setAttribute("cart",null);


        req.getRequestDispatcher(CONFIRM_ORDER_FORM).forward(req,resp);

//        String order_ShipToFirstName = req.getParameter("orderShipToFirstName");
//        session.setAttribute("orderShipToFirstName",order_ShipToFirstName);
//
//        String order_ShipToLastName = req.getParameter("orderShipToLastName");
//        session.setAttribute("orderShipToLastName",order_ShipToLastName);
//
//        String order_ShipAddress1 = req.getParameter("orderShipAddress1");
//        session.setAttribute("orderShipAddress1",order_ShipAddress1);
//
//        String order_ShipAddress2 = req.getParameter("orderShipAddress2");
//        session.setAttribute("orderShipAddress2",order_ShipAddress2);
//
//        String order_ShipCity = req.getParameter("orderShipCity");
//        session.setAttribute("orderShipCity",order_ShipCity);
//
//        String order_ShipState = req.getParameter("orderShipState");
//        session.setAttribute("orderShipState",order_ShipState);
//
//        String order_ShipZip = req.getParameter("orderShipZip");
//        session.setAttribute("orderShipZip",order_ShipZip);
//
//        String order_ShipCountry = req.getParameter("orderShipCountry");
//        session.setAttribute("orderShipCountry",order_ShipCountry);


    }

}
